package per.turtle.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.system.entity.SysDictType;
import per.turtle.system.service.ISysDictDataService;
import per.turtle.system.service.ISysDictTypeService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author turtle
 */
@AllArgsConstructor
@RequestMapping("dict/type")
@RestController
@Tag(name = "数据字典类型管理", description = "dictType_数据字典类型管理模块")
public class SysDictTypeController extends BaseController {

    private final ISysDictTypeService dictTypeService;

    private final ISysDictDataService dictDataService;

    @Operation(summary = "新增字典类型", description = "新增字典类型")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize(hasPermission = "system:dict:add")
    public CommonResult<Void> add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return CommonResult.error("新增字典【" + dict.getDictName() + "】失败，字典类型已存在");
        }
        return result(dictTypeService.insertDictType(dict));
    }

    @Operation(summary = "修改字典类型", description = "修改字典类型")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermission = "system:dict:edit")
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return CommonResult.error("修改字典【" + dict.getDictName() + "】失败，字典类型已存在");
        }
        return result(dictTypeService.updateDictType(dict));
    }

    @Operation(summary = "导出数据字典类型", description = "导出数据字典类型")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermission = "system:dict:export")
    @PostMapping("export")
    @ResponseExcel("字典类型")
    public List<SysDictType> export(HttpServletResponse response, @RequestBody SysDictType dictType) {
        return dictTypeService.selectDictTypeList(dictType);
    }

    @Operation(summary = "查询字典类型详细", description = "查询字典类型详细")
    @GetMapping("{dictId}")
    @PreAuthorize(hasPermission = "system:dict:query")
    public CommonResult<SysDictType> getInfo(@PathVariable Long dictId) {
        return CommonResult.success(dictTypeService.getById(dictId));
    }

    @Operation(summary = "获取数据字典类型列表或分页", description = "获取数据字典类型列表或分页（区别于是否传入分页参数）")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "system:dict:list")
    public TableDataInfo<SysDictType> list(@RequestBody SysDictType dictType) {
        List<SysDictType> list = startPage(s -> dictTypeService.selectDictTypeList(dictType));
        return getDataTable(list);
    }

    @Operation(summary = "获取字典选择框列表", description = "获取字典选择框列表")
    @GetMapping("optionSelect")
    public CommonResult<List<SysDictType>> optionSelect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return CommonResult.success(dictTypes);
    }

    @Operation(summary = "刷新字典缓存", description = "刷新字典缓存")
    @DeleteMapping("refreshCache")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @PreAuthorize(hasPermission = "system:dict:remove")
    public CommonResult<Void> refreshCache() {
        dictDataService.resetDictCache();
        return CommonResult.success();
    }

    @Operation(summary = "删除字典类型", description = "删除字典类型")
    @DeleteMapping("{dictIds}")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "system:dict:remove")
    public CommonResult<Void> remove(@PathVariable Long[] dictIds) {
        for (Long dictId : dictIds) {
            dictTypeService.deleteDictTypeById(dictId);
        }
        return CommonResult.success();
    }

}
